
import EventManager from './core/EventManager';
import GameData, { NumData } from './GameData';
import RESSpriteFrame from './RESSpriteFrame';
import Utils from './utils/Utils';

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class BallItem extends cc.Component {
    @property(cc.Node)
    public quan: cc.Node = null;
    @property(cc.Node)
    public iconImg: cc.Node = null;
    @property(cc.Node)
    public zimu: cc.Node = null;
    @property(cc.Node)
    public numNode: cc.Node = null;


    /**数据 */
    public _data: NumData;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        // this.numLabelNode = this.node.getChildByName('numLabel');
        // this.numLabel = this.numLabelNode.getComponent(cc.Label);
        let numLabel: cc.Label = this.numNode.getComponent(cc.Label);
        numLabel['_isBold'] = true;
        numLabel.cacheMode = cc.Label.CacheMode.BITMAP;
        let zimuLabel: cc.Label = this.zimu.getComponent(cc.Label);
        zimuLabel['_isBold'] = true;
        zimuLabel.cacheMode = cc.Label.CacheMode.BITMAP;
    }

    start() {

        // this.node.on(cc.Node.EventType.TOUCH_START, this.onClcik, this)
    }


    /**设置数据 */
    public set data(data: NumData) {
        if (!data) return;
        this._data = data;
        this.iconImg.getComponent(cc.Sprite).spriteFrame = RESSpriteFrame.instance.ballList[data.type - 1];

        this.zimu.getComponent(cc.Label).string = GameData.ZiMuString[data.type - 1];
        this.numNode.getComponent(cc.Label).string = `${data.num}`;
    }

    public get data() {
        return this._data;
    }


    public get num() {
        if (this.data) {
            return this.data.num;
        } else {
            return -1;
        }
    }

    private tween: cc.Tween;
    /**播放
     * 
     */
    public play(time: number) {
        console.log("time", time)
        this.quan.active = true;
        let quan: cc.Sprite = this.quan.getComponent(cc.Sprite);
        quan.fillRange = 1;
        this.tween = cc.tween(quan).to(time, { fillRange: 0 }).call(() => {
            this.quan.active = false;
            this.node.scale = 1;
            EventManager.emit("createBall")
        }, this).start();
    }


    public stop() {
        if (this.tween) {
            this.tween.stop();
        }
        this.quan.active = false;
        this.node.scale = 1;
    }


    public stopTween() {
        this.node.stopAllActions();
    }


    /**重置 */
    public reset() {
        this.node.scale = 1;
        this.node.opacity = 255;
    }

    // update (dt) {}
}


